﻿using System;
using System.Diagnostics;
using System.Linq;
using System.ServiceModel;
using System.ServiceProcess;
using System.Threading.Tasks;

namespace MyHome.Server.Service
{
    /// <summary>
    /// A service to host the <see cref="PluginManagement.PluginHost"/>.
    /// </summary>
    public partial class MyHomeServer : ServiceBase
    {
        private EventLog eventLog;
        private const string EVENTLOG_NAME = "MyHome.Server";
        private const string EVENTLOG_SOURCE = "MyHome.Server.Service";
        //private PluginManagement.PluginHost pluginHost;
        private MyHome.Server.Net.IService wcfService;
        private ServiceHost wcfServiceHost;

        /// <summary>
        /// Initializes a new instance of the <see cref="MyHomeServer"/> class.
        /// </summary>
        public MyHomeServer()
        {
            InitializeComponent();
            if (!System.Diagnostics.EventLog.SourceExists(EVENTLOG_SOURCE))
            {
                System.Diagnostics.EventLog.CreateEventSource(
                    EVENTLOG_SOURCE, EVENTLOG_NAME);
            }
            eventLog = new EventLog(EVENTLOG_NAME) { Source = EVENTLOG_SOURCE };
            Trace.Listeners.Add(new EventLogTraceListener(eventLog));
        }

        protected override void OnStart(string[] args)
        {
            Start(args);
        }

        /// <summary>
        /// Starts the service.
        /// </summary>
        /// <param name="args">The arguments.</param>
        /// <remarks>
        /// This is used to enable starting this service outside af the normal service environment,
        /// like starting it from a console application.
        /// </remarks>
        public void Start(string[] args)
        {
            Trace.TraceInformation("Service starting");
            //pluginHost = new PluginManagement.PluginHost();
            //Task.WaitAll(pluginHost.LoadAsync());
            //Task.WaitAll(pluginHost.StartAsync());
            if (wcfServiceHost != null)
                wcfServiceHost.Close();

            wcfService = new MyHome.Server.Net.Service();
            wcfServiceHost = new ServiceHost(wcfService);
            wcfServiceHost.Open();

            //wcfService.LoadPlugins();
            //wcfService.StartPlugins();

            Trace.TraceInformation("Service started");
            Console.ReadLine();
        }

        protected override void OnStop()
        {
            Stop();
        }

        /// <summary>
        /// Stops the service.
        /// </summary>
        /// <param name="args">The arguments.</param>
        /// <remarks>
        /// This is used to enable stopping this service outside af the normal service environment,
        /// like stopping it from a console application.
        /// </remarks>
        public new void Stop()
        {
            Trace.TraceInformation("Service stopping");
            wcfService.StopPlugins();
            wcfServiceHost.Close();
            Trace.TraceInformation("Service stopped");
        }
    }
}